package com.suning.sawp.service.impl.esb;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.suning.rsc.dto.responsedto.MbfResponse;
import com.suning.rsc.httpservice.annotation.EsbEIHttpWired;
import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.aircondition.AirConditionConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.custcommission.CustCommissionInfo;
import com.suning.sawp.dto.esb.QueryCipherReq;
import com.suning.sawp.dto.esb.QueryCipherRes;
import com.suning.sawp.dto.esb.QueryUnionRewardReq;
import com.suning.sawp.dto.esb.QueryUnionRewardRes;
import com.suning.sawp.intf.esb.CustCommissionService;
import com.suning.sawp.intf.esb.QueryCipherEsbService;
import com.suning.sawp.intf.esb.QueryRewardOfMTSEsbService;

/**
 * 
 * 会员佣金
 *
 * @author 15050769
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service("custCommissionService")
public class CustCommissionServiceImpl implements CustCommissionService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustCommissionServiceImpl.class);

    private QueryCipherEsbService queryCipherEsbService;

    private QueryRewardOfMTSEsbService queryRewardOfMTSEsbService;

    private static final String SUCCESS = "1";

    @Override
    public ReturnMsg<List<CustCommissionInfo>> getCustCommission(String custNum, String deviceNo) {
        ReturnMsg<List<CustCommissionInfo>> returnMsg = new ReturnMsg<List<CustCommissionInfo>>();
        LOGGER.info(" CustCommissionServiceImpl getCustCommission, custNum:{}, deviceNo:{}", custNum, deviceNo);
        QueryCipherReq queryCipherReq = new QueryCipherReq();
        queryCipherReq.setCustNum(custNum);
        queryCipherReq.setDeviceNo(deviceNo);

        // 调用ESB接口 查询暗号
        QueryCipherRes queryCipherRes = null;
        LOGGER.info(" CustCommissionServiceImpl queryCipher, custNum:{}, deviceNo:{}", custNum, deviceNo);
        MbfResponse resp = queryCipherEsbService.queryCipher(queryCipherReq);
        // 正常处理后, 进行数据类型转换
        if (null != resp && resp.isSucceed()) {
            try {
                queryCipherRes = (QueryCipherRes) resp.getOutput().getMbfBody(QueryCipherRes.class);
                if (queryCipherRes != null && SUCCESS.equals(queryCipherRes.getCode())) {
                    queryCipherRes.setSuccess(true);
                }
                LOGGER.info(" CustCommissionServiceImpl queryCipher,queryCipherRes:" + queryCipherRes);
            } catch (Exception e) {
                LOGGER.error("转换对象失败, ", e);
            }
        }

        if (queryCipherRes == null || !queryCipherRes.isSuccess()) {
            LOGGER.error(" CustCommissionServiceImpl queryCipher fail, custNum:{}, deviceNo:{}", custNum, deviceNo);
            returnMsg.setError(ErrorCodeConstants.ERR_0028);
            return returnMsg;
        }

        // 调用ESB接口 查询暗号
        QueryUnionRewardReq queryRewardReq = new QueryUnionRewardReq();
        queryRewardReq.setCipher(queryCipherRes.getCipher());
        queryRewardReq.setCustNum(custNum);
        queryRewardReq.setDeviceNo(deviceNo);
        queryRewardReq.setType("1");
        QueryUnionRewardRes queryUnionRewardRes = null;
        LOGGER.info(" CustCommissionServiceImpl queryUnionReward, custNum:{}, deviceNo:{}", custNum, deviceNo);
        MbfResponse mbfResponse = queryRewardOfMTSEsbService.queryUnionReward(queryRewardReq);
        // 正常处理后, 进行数据类型转换
        if (null != mbfResponse && mbfResponse.isSucceed()) {
            try {
                queryUnionRewardRes = (QueryUnionRewardRes) mbfResponse.getOutput().getMbfBody(
                        QueryUnionRewardRes.class);
                if (queryUnionRewardRes != null && SUCCESS.equals(queryUnionRewardRes.getCode())) {
                    queryUnionRewardRes.setSuccess(true);
                }
                LOGGER.info(" CustCommissionServiceImpl queryCipherRes,queryUnionRewardRes:" + queryUnionRewardRes);
            } catch (Exception e) {
                LOGGER.error("转换对象失败, ", e);
            }
        }

        if (queryUnionRewardRes == null || !queryUnionRewardRes.isSuccess()) {
            LOGGER.error(" CustCommissionServiceImpl queryUnionRewardRes fail, custNum:{}, deviceNo:{}", custNum,
                    deviceNo);
            returnMsg.setError(ErrorCodeConstants.ERR_0028);
            return returnMsg;
        }

        // 取得系统当前时间
        Calendar cal = Calendar.getInstance();

        List<CustCommissionInfo> commList = new ArrayList<CustCommissionInfo>();
        // 设置上上月的数据
        CustCommissionInfo bfrLastInfo = new CustCommissionInfo();
        //由于客户端显示错误，将CPA数据和CPS数据交换下。
        // CPA奖励金额
//        bfrLastInfo.setCPA(queryUnionRewardRes.getBfrLastCpaReward());
        bfrLastInfo.setCPA(queryUnionRewardRes.getBfrLastCpsReward());
        // CPS奖励金额
//        bfrLastInfo.setCPS(queryUnionRewardRes.getBfrLastCpsReward());
        bfrLastInfo.setCPS(queryUnionRewardRes.getBfrLastCpaReward());
        // 首单
        bfrLastInfo.setFristOrder(queryUnionRewardRes.getBfrLastFirstReward());
        // 获取上上月的月份
        cal.add(Calendar.MONTH, -2);
        bfrLastInfo.setMonthName(cal.get(Calendar.YEAR)+ "年"+ AirConditionConstants.MONTH_MAP.get(cal.get(Calendar.MONTH)+ 1));
        bfrLastInfo.setTotalCommission(queryUnionRewardRes.getBfrLastReward());


        // 设置上月数据
        CustCommissionInfo lastInfo = new CustCommissionInfo();
        // CPA奖励金额
//        lastInfo.setCPA(queryUnionRewardRes.getLastCpaReward());
        lastInfo.setCPA(queryUnionRewardRes.getLastCpsReward());
        // CPS奖励金额
//        lastInfo.setCPS(queryUnionRewardRes.getLastCpsReward());
        lastInfo.setCPS(queryUnionRewardRes.getLastCpaReward());
        // 首单
        lastInfo.setFristOrder(queryUnionRewardRes.getLastFirstReward());
        // 获取上月的月份
        cal.add(Calendar.MONTH, 1);
        lastInfo.setMonthName(cal.get(Calendar.YEAR)+ "年"+ AirConditionConstants.MONTH_MAP.get(cal.get(Calendar.MONTH)+ 1));
        lastInfo.setTotalCommission(queryUnionRewardRes.getLastReward());


        // 设置本月数据
        CustCommissionInfo commInfo = new CustCommissionInfo();
        // CPA奖励金额
//        commInfo.setCPA(queryUnionRewardRes.getCurrCpaReward());
        commInfo.setCPA(queryUnionRewardRes.getCurrCpsReward());
        // CPS奖励金额
//        commInfo.setCPS(queryUnionRewardRes.getCurrCpsReward());
        commInfo.setCPS(queryUnionRewardRes.getCurrCpaReward());
        // 首单
        commInfo.setFristOrder(queryUnionRewardRes.getCurrFirstReward());
        // 获取本月的月份
        cal.add(Calendar.MONTH, 1);
        commInfo.setMonthName(cal.get(Calendar.YEAR)+ "年"+ AirConditionConstants.MONTH_MAP.get(cal.get(Calendar.MONTH)+ 1));
        commInfo.setTotalCommission(queryUnionRewardRes.getCurrReward());
        
        commList.add(commInfo);
        commList.add(lastInfo);
        commList.add(bfrLastInfo);

        returnMsg.setData(commList);
        return returnMsg;
    }

    public QueryRewardOfMTSEsbService getQueryRewardOfMTSEsbService() {
        return queryRewardOfMTSEsbService;
    }

    @EsbEIHttpWired
    public void setQueryRewardOfMTSEsbService(QueryRewardOfMTSEsbService queryRewardOfMTSEsbService) {
        this.queryRewardOfMTSEsbService = queryRewardOfMTSEsbService;
    }

    public QueryCipherEsbService getQueryCipherEsbService() {
        return queryCipherEsbService;
    }

    @EsbEIHttpWired
    public void setQueryCipherEsbService(QueryCipherEsbService queryCipherEsbService) {
        this.queryCipherEsbService = queryCipherEsbService;
    }

    public static void main(String[] args) {
        // 取得系统当前时间
        Calendar cal = Calendar.getInstance();
        // 日期减一,取得上月最后一天时间对象
        cal.add(Calendar.MONTH, -2);
        // 输出上月最后一天日期
        System.out.println(cal.get(Calendar.MONTH));
        System.out.println(cal.get(Calendar.YEAR)+ "年"+ AirConditionConstants.MONTH_MAP.get(cal.get(Calendar.MONTH) + 1));
        
//        Calendar ca1l = Calendar.getInstance();
        cal.add(Calendar.MONTH, 1);
        // 输出上月最后一天日期
        System.out.println(cal.get(Calendar.MONTH));
        System.out.println(cal.get(Calendar.YEAR)+ "年"+ AirConditionConstants.MONTH_MAP.get(cal.get(Calendar.MONTH)+ 1));
        
//        Calendar ca12= Calendar.getInstance();
        cal.add(Calendar.MONTH, 1);
        // 输出上月最后一天日期
        System.out.println(cal.get(Calendar.MONTH));
        System.out.println(cal.get(Calendar.YEAR)+ "年"+ AirConditionConstants.MONTH_MAP.get(cal.get(Calendar.MONTH)+ 1));
    }
}
